package v1

import (
	"ginblog/api/common"
	"ginblog/model"
	"ginblog/tools/upload"
	"ginblog/utils/errmsg"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

func UploadRes(c *gin.Context) {
	u := upload.Upload{}
	file, fileHeader, _ := c.Request.FormFile("file")
	u.File = file
	u.FileSize = fileHeader.Size
	flag, fileUrl := u.UploadFile()
	if !flag {
		common.FailJson(c, u.GetCode())
		return
	}
	R := model.Resources{}
	_ = c.ShouldBindBodyWith(&R, binding.JSON)
	R.ResourcesUrl = fileUrl
	if !R.AddRes() {
		if R.GetCode() != 0 {
			common.FailJson(c, R.GetCode())
			return
		} else {
			common.FailJson(c, errmsg.ERROR)
			return
		}
	}
	common.SuccessJson(c, errmsg.SUCCESS, R)
	return
}
